package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Order;
import com.powernode.model.OrderConfirmParam;
import com.powernode.model.Result;
import com.powernode.service.OrderService;
import com.powernode.util.AuthUtils;
import com.powernode.vo.OrderStatusCount;
import com.powernode.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购物系统：订单业务控制层
 */
@Api(tags = "购物系统：订单业务控制层")
@RequestMapping("p/myOrder")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("查询用户订单状态数量对象")
    @GetMapping("orderCount")
    public Result<OrderStatusCount> loadUserOrderStatusCount() {
        String openId = AuthUtils.getLoginUserOpenId();
        OrderStatusCount orderStatusCount = orderService.queryUserOrderStatusCount(openId);
        return Result.success(orderStatusCount);
    }

//    p/myOrder/myOrder?current=1&size=10&status=0
    @ApiOperation("根据订单状态分页查询会员订单列表")
    @GetMapping("myOrder")
    public Result<Page<Order>> loadMemberOrderPage(Page<Order> page,Integer status) {
        page = orderService.queryMemberOrderPageByStatus(page,status);
        return Result.success(page);
    }

//    p/myOrder/orderDetail?orderNumber=1544203451372802048
    @ApiOperation("根据订单编号查询会员订单详情")
    @GetMapping("orderDetail")
    public Result<Order> loadMemberOrderDetail(@RequestParam String orderNumber) {
        Order order = orderService.queryMemberOrderDetailByOrderNumber(orderNumber);
        return Result.success(order);
    }

//    p/myOrder/cancel/1544222605400215552
    @ApiOperation("会员取消订单")
    @PutMapping("cancel/{orderNumber}")
    public Result<String> cancelMemberOrder(@PathVariable String orderNumber) {
        Boolean canceled = orderService.cancelMemberOrderByOrderNumber(orderNumber);
        return Result.handle(canceled);
    }

//    p/myOrder/1544222605400215552
    @ApiOperation("根据订单编号删除订单记录")
    @DeleteMapping("{orderNumber}")
    public Result<String> removeOrder(@PathVariable String orderNumber) {
        Boolean removed = orderService.removeOrderByOrderNumber(orderNumber);
        return Result.handle(removed);
    }

//    p/myOrder/receipt/1544227267721433088
    @ApiOperation("会员确认收货")
    @PutMapping("receipt/{orderNumber}")
    public Result<String> receiptMemberOrder(@PathVariable String orderNumber) {
        Boolean receipted = orderService.receiptMemberOrder(orderNumber);
        return Result.handle(receipted);
    }

//    p/myOrder/confirm
    @ApiOperation("查询会员订单确认页面数据")
    @PostMapping("confirm")
    public Result<OrderVo> loadMemberOrderVo(@RequestBody OrderConfirmParam orderConfirmParam) {
        OrderVo orderVo = orderService.queryMemberOrderVo(orderConfirmParam);
        return Result.success(orderVo);
    }

//    p/myOrder/submit
    @ApiOperation("会员提交订单")
    @PostMapping("submit")
    public Result<String> submitMemberOrder(@RequestBody OrderVo orderVo) {
        String orderNumber = orderService.submitMemberOrder(orderVo);
        return Result.success(orderNumber);
    }

    @ApiOperation("根据订单流水号获取订单信息")
    @GetMapping("getByOrderNumber/{orderNumber}")
    public Result<Order> getByOrderNumber(@PathVariable("orderNumber") String orderNumber) {
        Order order = orderService.getByOrderNumber(orderNumber);
        return Result.success(order);
    }
}
